﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using System.Data;

namespace SPSocialUtil.SocialBookmark
{
    /// <summary>
    /// 소셜 태그를 활용한 북마크 매니저를 제공한다
    /// 하는 일
    /// - 개인의 북마크(선호태그)를 추가한다
    /// - 개인의 북마크(선호태그)를 삭제한다
    /// - 개인의 북마크 목록을 제공한다
    /// </summary>
    public class BookmarkUtil
    {
        string siteUrl;
        string cUserLoginName;
        UserProfile uProfile;

        public BookmarkUtil(SPUser myUser, string siteCollUrl)
        {
            cUserLoginName = myUser.LoginName;
            siteUrl = siteCollUrl;
            using (SPSite site = new SPSite(siteUrl))
            {
                SPServiceContext sContext = SPServiceContext.GetContext(site);
                UserProfileManager uManager = new UserProfileManager(sContext);
                UserProfile uProfile = uManager.GetUserProfile(cUserLoginName);
            }
        }

        public void addNewBookmark(string bTitle, string bURL)
        {
            uProfile.PersonalizationLinks.Create(bTitle, new System.Uri(bURL));
            uProfile.Commit();
        }

        public void removeBookmark(string bURL)
        {
            foreach (PersonalizationLink tLink in uProfile.PersonalizationLinks.GetItems())
            {
                if (tLink.Url == bURL)
                {
                    tLink.Delete();
                    break;
                }
            }
            uProfile.Commit();
        }

        public DataSet getBookmarks()
        {
            return uProfile.PersonalizationLinks.GetDataSet();
        }
    }
}
